package icu.lookyousmileface.nio.basic;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @author shenj
 * @title: NioBufferOnlyRead
 * @projectName NettyPro
 * @date 2020/12/1 9:54
 */
public class NioBufferOnlyRead {

    public static void main(String[] args) {

        ByteBuffer dataBuffer = ByteBuffer.allocate(5);

        for (int i = 0; i < dataBuffer.capacity() - 1; i++) {
            dataBuffer.put((byte) (i * 2));
        }

        dataBuffer.flip();
        //可以从一个创建的Buffer获取OnlyReadBuffer
        ByteBuffer onlyByteBuffer = dataBuffer.asReadOnlyBuffer();

        while (onlyByteBuffer.hasRemaining()) {
            System.out.println(onlyByteBuffer.get());
        }
        //无法往OnlyReadBuffer写数据
//        onlyByteBuffer.put((byte)(2));
        //可以往dataBuffer中写数据
        dataBuffer.put((byte) (2));
    }
}
